#ifndef GAMEBOARDORGANIZER_H
#define GAMEBOARDORGANIZER_H

#include <QGraphicsObject>
#include <QDebug>
#include "gs.h"
using namespace gs;

namespace gs
{
class GameBoardOrganizer : public QGraphicsObject
{
    Q_OBJECT
public:
    GameBoardOrganizer(GameTable*);
    GameBoardOrganizer(GameTable*, GameBoardOrganizer*);
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    GameTable* getGameTable(){return _table;}
    GameBoardOrganizer* getParentBoard(){return _board;}
protected:
    GameTable* _table;
    GameBoardOrganizer* _board;
};


class GhostBoard : public GameBoardOrganizer
{
    Q_OBJECT
public:
    GhostBoard(GameTable*, GameBoardOrganizer*, GSColor);
    void setId(int);
    void setPlayer(Player*);
    int  getId() { return _id; }
    QColor getColor();
    GSColor getGSColor();
    QPixmap getPlayerPixmap();
    QRectF getPlayerInfoBound(PlayerBriefInfoUp*);
    Player* getPlayer();
    QList<GhostSlot*> getSlots(){return slot;}
    int getBoardIndex();
    bool skillSealed() { return _seal_skill_counter > 0; }
    void sealSkill()
    {
        if( _seal_skill_counter ++ == 0 )
        {
            emit skill_sealed();
        }
    }
    void releaseSkill()
    {
        if( -- _seal_skill_counter == 0 )
            emit skill_released();
    }
protected:
    GhostBoardBottom* bottom;
    GhostBoardTop* top;
    PlayerHeadArea* phead;
    QList<PlayerHeadQi*> qi;
    QList<GhostSlot*> slot;
    VillageTile * tiles [3]; // to do
    GSColor _c;
    int _id;
    Player* _p;
    int _seal_skill_counter;
signals:
    void clicked(GhostSlot*);
    void skill_sealed();
    void skill_released();
};

class TileBoard : public GameBoardOrganizer
{
    Q_OBJECT
public:
    TileBoard(GameTable*, GameBoardOrganizer*);
    QList<VillageTile*> getVillages() {return villages;}
    VillageTile * getVillageAt( int, int );
protected:
    QList<VillageTile*> villages;
    int indices [3][3];
signals:
    void clicked(VillageTile*);
};

class CenterTable : public GameBoardOrganizer
{
    Q_OBJECT
public:
    CenterTable(GameTable*);
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    void createBoards();
    void setBoardPos();
    void setBoardId();
    void distributeBoardToPlayer();
    pIconOrganizer* getIconOranizer() {return playerIcons;}
    TileBoard* getTileBoard() {return tileBoard;}
    QList<GhostBoard*> getGhostBoards(){return board;}
    static const int WIDTH = 480;
    static const int HEIGHT = 480;
    static const int OUTWIDTH = WIDTH+10;
    static const int OUTHEIGHT = HEIGHT+10;
protected:
    QList<GhostBoard*> board;
    TileBoard* tileBoard;
    pIconOrganizer* playerIcons;
signals:
    void clicked(GhostSlot*);
    void clicked(VillageTile*);
};

class pIconOrganizer : public GameBoardOrganizer
{
public:
    pIconOrganizer(GameTable*, GameBoardOrganizer*);
    void setIconsOnBoard();
    QList<PlayerIcon*> getPlayerIcons() {return iconItems; }
private:
    QList<Player*> _pls;
    QPixmap _redPix;
    QPixmap _yellowPix;
    QPixmap _greenPix;
    QPixmap _bluePix;
    QList<PlayerIcon*> iconItems;
    static const int EDGE_GAP = 5;
};


class LeftTable : public GameBoardOrganizer
{
public:
    LeftTable(GameTable*);
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    InfoBoard * getInfoBoard() { return _info_board; }
    static const int WIDTH = 180;
    static const int HEIGHT = 480;
    static const int OUTWIDTH = WIDTH+10;
    static const int OUTHEIGHT = HEIGHT+10;
protected:
    InfoBoard * _info_board;
};

class RightTable : public GameBoardOrganizer
{
public:
    RightTable(GameTable*);
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    DeckBoard * getDeckBoard() { return _deck_board; }
    static const int WIDTH = 180;
    static const int HEIGHT = 480;
    static const int OUTWIDTH = WIDTH+10;
    static const int OUTHEIGHT = HEIGHT+10;
protected:
    DeckBoard * _deck_board;
};


class BottomTable : public GameBoardOrganizer
{
    Q_OBJECT
public:
    BottomTable(GameTable*);
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    static const int WIDTH = 750;
    static const int HEIGHT = 50;
    static const int OUTWIDTH = WIDTH+10;
    static const int OUTHEIGHT = HEIGHT+10;
protected:
    PlayerTaoField* TaoField;
    PlayerBasic* BasicField;
    PlayerAction* ActionField;
    Player* _pl;
};
}

#endif // GAMEBOARDORGANIZER_H
